package com.nfit.shopadmin.controller;

import com.nfit.shopadmin.service.CartService;
import com.nfit.shopmodel.dto.cart.CartDto;
import com.nfit.shopmodel.entity.cart.CartItem;
import com.nfit.shopmodel.vo.common.Result;
import com.nfit.shopmodel.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ClassName:CartController
 * Package:com.nfit.shopadmin.controller
 * Description
 *
 * @Author L
 * @Create 2024/12/31 21:22
 */
@Slf4j
@Tag(name = "购物车")
@RestController
@RequestMapping("/admin/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     */
    @PostMapping("/saveCart")
    public Result saveCart(@RequestBody CartDto cartDto){
        cartService.saveCart(cartDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取购物车列表
     */
    @GetMapping("/getCartList/{userId}")
    public Result getCartList(@PathVariable("userId") Long userId){
       List<CartItem> list = cartService.getCartList(userId);
//       log.info("获取购物车列表:{}",list);
       return Result.build(list,ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据购物车id删除
     */
    @DeleteMapping("/remove/{id}")
    public Result removeCart(@PathVariable("id") Long id){
        cartService.removeCart(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改商品库存信息
     * @param skuId skuId
     * @param count 用户购买商品数量
     */
    @PutMapping("/modifyProductStock/{skuId}/{count}")
    public Result modifyProductStock(@PathVariable("skuId") Long skuId,
                                     @PathVariable("count") Integer count){
        cartService.modifyProductStock(skuId,count);
//        log.info("商品sku:{}和购买数量：{}",skuId,count);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改购物车商品选中状态-单品商品
     */
    @PostMapping("/modifySelected")
    public Result modifySelected(@RequestBody CartDto cartDto){
//        log.info("skuId：{}--选中状态值：{}",cartDto.getSkuId(),cartDto.getSelectedStatus());
        cartService.modifySelected(cartDto);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改购物车商品选中状态-单品商品
     */
    @PutMapping("/modifySelectedAll")
    public Result modifySelectedAll(@RequestBody Map<String,Boolean> map){
        cartService.modifySelectedAll(map);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

}
